﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    globalcenter.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-11-04  00:04
*/
#include "databasecenter.h"
#include <QDebug>

QMutex DatabaseCenter::mutex;
DatabaseCenter* DatabaseCenter::instance = nullptr;

DatabaseCenter& DatabaseCenter::getInstance()
{
    if (instance == nullptr) {
        QMutexLocker locker(&mutex);
        if (instance == nullptr) {
            instance = new DatabaseCenter();
        }
    }
    return *instance;
}

void DatabaseCenter::deleteInstance()
{
    if (instance != nullptr) {
        delete instance;
        instance = nullptr;
    }
}

QSqlDatabase DatabaseCenter::getConnection()
{
    return databasePool->getConnection();
}

void DatabaseCenter::releaseConnection(QSqlDatabase &connection)
{
    databasePool->releaseConnection(connection);
}

DatabaseCenter::DatabaseCenter(QObject *parent) : QObject(parent)
{
    QString databasePoolConfigFileName = QCoreApplication::applicationDirPath() + "/config/database.ini";
    databasePoolSettings = new QSettings(databasePoolConfigFileName,QSettings::IniFormat, this);
    databasePoolSettings->beginGroup("database");
    databasePool = new QDatabasePool(databasePoolSettings);
}

DatabaseCenter::~DatabaseCenter()
{
    delete databasePool;
    delete databasePoolSettings;
}

